var default_source_language = '';
var dom_trans_storage = "#gTranslateStorage";

google.load("language", "1");

function storeElement(selector){
	store = jq(dom_trans_storage, selector);
	if (store.children().length == 0){
		jq(store).html(selector.html());
	}

}

function restoreElement(selector){
	stored_element = jq(dom_trans_storage, jq(selector));
	if(stored_element.children() > 0){
		return stored_element.html();
	}else{
		return 0;
	}
}

function translate_element(element, source_lang, target_lang){
	text = jq(element).html();
	google.language.translate(text, source_lang, target_lang, function(res){
			if(!res.error){
				jq(element).html(res.translation);
			}
		});
}

function translate_comment(lang_code, container){
	cm_items = ["span.commentByLine", "div.commentBody",
	"div.commentactionbuttonsrow"]
	original_element = container;
	for (index in cm_items){
		the_item = cm_items[index];
		
		el = jq(the_item, jq(original_element));
		if (el.length){
			translate_element(el, default_source_language,lang_code);
		}
	}
}


function t_comment(el){
	link_element = jq(el);
	target_language = link_element.attr('rel');
	comment_container = jq(link_element).parent().parent().parent();
	translate_comment(target_language, comment_container);
}


